make_layers <- function(df){
  n <- max(df$order)
  coords <- vector(mode = "complex", length = n)
  colors <- vector(mode = "complex", length = n)
  df_list = mget(ls(pattern = "new_df[1-2]"))
  
  for (i in 1:n){
    new_df <- df %>%
      filter(order == i) %>%
      mutate(
        color_order = row_number(),
        color = fct_reorder(color, color_order))
    
    cols <- as.character(new_df$color)
    coords[[i]] <- list(new_df)
    colors[[i]] <- list(cols)
  }
  
  return(c(list(coords), list(colors)))
}
circles <- tribble(~x, ~y, ~shape, ~color, ~radius, ~order, ~color_cat, ~grouping,
                30, 69, "fill", "#583B76", 6, 1, "purple", 1,
                30, 69, "fill", "#0F0F11", 12, 2, "black", 1,
                30, 69, "fill", "#CA7A7C", 14, 4, "pink", 1,
                30, 69, "fill", "#CB6A5A", 15, 5, "red", 1,
                39, 57, "fill", "#A42117", 5, 3, "red", 2,
                39.5, 56.5, "fill", "#E2BF50", 7.5, 6, "yellow", 2,
                27, 25, "fill", "#CCB644", 4.5, 1, "yellow", 3,
                27, 25, "stroke", "#0C0E18", 4.7, 2, "black", 3,
                27, 25, "fill", "#6E93B6", 6, 3, "blue", 3,
                27, 25, "fill", "#9E98B2", 7.3, 4, "purple", 3,
                62, 11.5, "fill", "#2F5581", 3.2, 1, "blue", 4,
                62.5, 11, "fill", "#D4A590", 4.5, 2, "orange", 4,
                62.5, 11, "fill", "#CEBF4A", 5.5, 3, "yellow", 4,
                121, 14, "fill", "#73776D", 5.5, 1, "grey", 5,
                106.5, 20, "fill", "#7F67A8", 3.5, 1, "purple", 6,
                75.5, 60, "fill", "#F7F4F5", 2.2, 1, "white", 7,
                75.5, 60, "stroke", "#701A23", 2.5, 2, "red", 7,
                75.5, 60, "stroke", "#010601", 3, 3, "black", 7,
                75.5, 60, "fill", "#F5F0F3", 4.3, 4, "white", 7,
                75.5, 60, "stroke", "#A19E9B", 4.4, 5, "black", 7,
                75.5, 69, "fill", "#BB3247", 1, 1, "red", 7,
                75.5, 69, "stroke", "#202222", 1.2, 2, "black", 7,
                82, 78, "fill", "#80808C", 1.5, 1, "grey", 8,
                88, 65, "fill", "#FFFEFD", 1.5, 1, "white", 9,
                88, 65, "stroke", "#666561", 1.7, 2, "grey", 9,
                88, 65, "fill", "#B3BBB2", 2.5, 3, "green", 9,
                96, 73.5, "fill", "#2A2981", 1.3, 1, "blue", 10,
                96, 73.5, "fill", "#CBC5C0", 1.8, 2, "grey", 10,
                96, 73.5, "stroke", "#2E2A28", 2, 3, "black", 10,
                113, 49, "fill", "#375A99", 4, 1, "blue", 11,
                124, 63.5, "fill", "#A88698", 2.5, 1, "purple", 12,
                124.3, 63.2, "fill", "#C64536", 2.9, 2, "red", 12,
                124, 70.5, "fill", "#7697CF", 2.2, 1, "blue", 13,
                124, 70.5, "fill", "#DDCC77", 2.8, 2, "yellow", 13,
                132.5, 68.5, "fill", "#F2EDF1", 2, 1, "white", 14,
                132.5, 68.5, "stroke", "#0C0908", 2.2, 2, "black", 14,
                #this one is special and needs to actually be stroke
                109, 68, "stroke", "#080A08", 4.2, 3, "black", 15,
                111, 68, "fill", "#95B29C", 3.5, 1, "green", 16,
                109, 68, "fill", "#C7AC60", 4, 2, "yellow", 15,
                97, 46, "fill", "#050405", 0.5, 1, "black", 17,
                96.5, 46, "fill", "#E7DEDC", 3, 2, "white", 17,
                96.5, 46, "fill", "#33388F", 3.7, 3, "blue", 17,
                96.5, 46, "stroke", "#170E01", 3.9, 4, "black", 17
                ) %>%
  mutate(id = row_number(),
         alpha = ifelse(id == 40, 2, 1))

circles_l <- make_layers(circles)
id <- factor(c(1:16))

shapes <- data.frame(
  id = id,
  color = c("#90AFC7", "#234533", "#E3D2D3", "#9E7237", "#E6D54B", "#8C898A", "#0B0B0B", "#B4B76F", "#CB3121",
            "#C12B1D", "#C92B1F", "#B89D42", "#AE774C", "#B26A2D", "#744C1A", "#814819"),
  color_cat = c("blue", "green", "white", "brown", "yellow", "grey", "black", "green", "red", "red", "red",
                     "yellow", "brown", "brown", "brown", "brown"),
  order = c(4, 6, 5, 3, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1),
  alpha = rep(1, 16))

coords <- data.frame(
  id = rep(id, each = 3),
  x = c(36, 55.5, 90, 80, 107, 99, 73, 104, 88, 80, 100.5, 97, 89, 94, 92, 88.5, 96, 89, 97.5, 104.3, 103.5, 
        98, 107.5, 108.5, 93.2, 96, 93.5, 99, 97.5, 98.6, 99.2, 98, 99.5, 112, 122.5, 120, 112.5, 134, 111.5,
        112, 126.5, 113.2, 111, 124, 112.3, 109.5, 125, 110.6),
  y = c(44, 64.5, 34, 39.5, 39.5, 52.5, 28, 28, 84, 39.5, 39.5, 51, 75, 75, 79.5, 48.3, 46.5, 50, 47, 54, 55,
        45, 33, 34, 47.2, 46.5, 47.7, 48.3, 47, 48.5, 43.3, 45, 43.5, 19, 16, 28, 59, 73, 61,
        57, 33, 57.5, 51.5, 32, 52, 50.5, 27, 51)
)

triangles <- full_join(coords, shapes, by = "id") %>%
  mutate(grouping = id)

triangles_l <- make_layers(triangles)
id <- factor(c(1:84))

shapes <- data.frame(
  id = id,
  color = c("#0C0C0F", "#7A521E", "#F4E4E2", "#4F652D", "#DED5D3", "#070709", 
            # new row
            "#B4BDC7", "#09090B", "#DFD8D4", "#6D93AE", "#DBD3CA", "#0E0E11", "#D9D3BF", "#0B0B0D",
            # new row
            "#E9D5D0", "#CCCBB0", "#DFD6D1", "#E0DACC", "#40664B", "#DBD2CC", "#101014", "#E0D8D6", "#D5C6C1",
            "#D9D2C7", "#0C0C0E",
            # new row
            "#7B9248", "#E7DFDD", "#D1D4CE", "#E1A1A0", "#C9BDB3", "#111013", "#D4CCC2", "#855A20",
            "#D8D0CA", "#101414", "#D4CBC3", "#09080B", "#D5CEC9", "#141416", "#DFD9D3", "#0A0B0D",
            # other squares
            "#7B1B1C", "#E2D299", "#DA90A4", "#424242", "#050507", "#0D0C11", "#07030C", "#0C0A0F", "#1C1918", "#000000",
            # hashtag
            "#EBB2A8", "#E8E8ED", "#C54D54", "#DAD7DA",
            # checkers upper right
            "#727070", "#0C0D10", "#E7D0D2", "#9A9393", "#17161D", 
            # checker background
            "#E6E0E2",
            # next row
            "#B52634", "#555553", "#0A0A0C", "#BCB9B6", "#B3ACAB",
            # next rows
            "#805F4A", "#060508", "#D9CDC6", "#6A7859", "#354347", "#625B56",
            # top row 3 + 1
            "#353132", "#858590", "#807E88", "#05080C",
            # diagonal strip
            "#111015", "#D4C9C9", "#747065", "#0F0E0F", "#A7241C", "#030306",
            # boomerang
            "#1C1C20", "#55724B"
            ),
  color_cat = c("black", "brown", "white", "green", "white", "black", 
                     # new row
                     "grey", "black", "white", "blue", "white", "black", "white", "black",
                     # new row
                     "white", "white", "white", "white", "green", "white", "black", "white", "white", "white", "black",
                     # new row
                     "green", "white", "white", "pink", "white", "black", "white", "brown", "white", "black",
                     "white", "black", "white", "black", "white", "black",
                     # other squares
                     "red", "yellow", "pink", "black", "black", "black", "black", "black", "black", "black",
                     # hashtag
                     "pink", "white", "pink", "white",
                     # checkers upper right
                     "grey", "black", "pink", "grey", "black",
                     # checker background
                     "white",
                     # next row
                     "red", "grey", "black", "grey", "grey",
                     # next rows
                     "brown", "black", "white", "green", "green", "grey",
                     # top row 3 + 1
                     "grey", "grey", "grey", "black",
                     # diagonal strip
                     "black", "white", "grey", "black", "red", "black",
                     # boomerang
                     "black", "green"
                     ),
  order = c(rep(1, 49), rep(2, 2), rep(1, 6), 2, rep(1, 2), 4, rep(1, 6), 3, rep(1, 16)),
  alpha = c(rep(1, 84)),
  grouping = c(rep(1, 41), 2:9, 2, 3, rep(10, 4), rep(11, 21), rep(12, 6), rep(13, 2)))

coords <- data.frame(
  id = c(rep(id[1:60], each = 4), rep(61, 14), rep(id[62:84], each = 4)),
  x = c(30.5, 31, 51, 50, 42, 44, 48, 46, 44, 45, 50, 48, 45, 46.5, 52.5, 50, 46.5, 47.5, 54.5, 52.5, 
        47.5, 49.5, 56, 54.5, 46, 48, 49.5, 46.5, 48, 50, 52, 49.5, 50, 52.5, 55, 52, 52.5, 54.5, 57.5, 55,
        54.5, 56, 59, 57.5,
        56, 57, 60, 59, 
        57, 58, 61.5, 60,
        58, 59, 63, 61.5,
        # new row
        46.5, 49.5, 51, 48.5,
        49.5, 52, 53.5, 51,
        52, 55, 57, 53.5,
        55, 57.5, 59, 57,
        57.5, 59, 60, 59,
        59, 60, 61.5, 60,
        60, 61.5, 62.5, 61.5,
        61.5, 63, 64, 62.5,
        63, 66, 67, 64,
        66, 68, 69, 67,
        68, 70, 70.5, 69,
        # new row
        48.5, 51, 53, 51,
        51, 53.5, 55.5, 53,
        53.5, 57, 59, 55.5,
        57, 59, 61, 59,
        59, 60, 61.5, 61,
        60, 61.5, 63, 61.5,
        61.5, 62.5, 64, 63,
        62.5, 64, 65, 64,
        64, 67, 68, 65,
        67, 69, 70, 68,
        69, 70.5, 72, 70,
        70.5, 72, 73, 72,
        72, 73.5, 74.5, 73,
        73.5, 74.5, 75.5, 74.5,
        74.5, 75.5, 76.5, 75.5,
        75.5, 77, 78, 76.5,
        # other squares
        125.5, 125.5, 130, 130,
        116, 116, 118, 118,
        100, 100, 107, 107,
        119.5, 122.5, 124, 121,
        122, 124.5, 126, 123,
        127, 129, 130, 128,
        127, 128.5, 129, 127.5,
        128, 129.5, 131, 129,
        125.3, 125.3, 130.3, 130.3,
        115.5, 115.5, 118.5, 118.5,
        # hashtag
        129, 129, 132, 132,
        129, 129, 132, 132,
        132, 132, 135, 135,
        132, 132, 135, 135,
        # upper right checker arrangement
        104, 105.5, 106.5, 105,
        106, 107.5, 108.5, 107,
        # next row
        105.5, 107, 108, 106.5,
        106.5, 108, 109, 107.5,
        108.5, 110, 111, 109.5,
        # checker background (14)
        104, 107, 105, 114.5, 112, 142, 151, 121.5, 123, 122, 122.5, 113, 114, 108,
        # next row
        102.5, 104.5, 105.5, 103.5,
        103.5, 105.5, 106.5, 105,
        106, 107.5, 108.5, 107,
        107, 108.5, 109.5, 108,
        109, 110.5, 111, 110,
        # next row
        104.5, 106, 107, 105.5,
        # SHOULD BE ON LAYER 3
        106.5, 108, 109, 107.5,
        # next row
        110, 111.5, 113, 112,
        # next row
        108.5, 111, 112, 109.5,
        # next row
        109.5, 112, 113, 111,
        112, 114.5, 116.5, 113,
        # top row 3 + 1
        119, 121, 122, 120,
        129, 131.5, 132.5, 130,
        138.5, 141, 142, 139.5,
        121.5, 124, 125, 122,
        # diagonal strip
        80.5, 82, 82.5, 81,
        82, 86.5, 87, 82.5,
        86.5, 94, 95, 87,
        94, 98, 98.5, 95,
        98, 110, 110.5, 98.5,
        110, 131, 131.5, 110.5,
        # boomerang
        75, 87.5, 106, 87.5,
        78, 87.5, 106, 87.7
        ),
  y = c(33, 34, 23, 22, 28, 34, 30, 26, 34, 37, 33, 30, 37, 39.5, 36, 33, 39.5, 43, 38.5, 36, 
        43, 47.5, 41, 38.5, 26, 30, 28.5, 25.5, 30, 33, 31.5, 28.5, 33, 36, 35, 31.5, 36, 38.5, 37, 35,
        38.5, 41, 38.5, 37,
        41, 43, 40, 38.5,
        43, 44, 41, 40,
        44, 46.5, 42.5, 41,
        # new row
        25.5, 28.5, 27, 24.5,
        28.5, 31.5, 30, 27,
        31.5, 35, 33.5, 30,
        35, 37, 36, 33.5,
        37, 38.5, 37, 36,
        38.5, 40, 38.5, 37,
        40, 41, 40, 38.5,
        41, 42.5, 41.5, 40,
        42.5, 45.5, 44.5, 41.5,
        45.5, 47.5, 47, 44.5,
        47.5, 49.5, 49, 47,
        # new row
        24.5, 27, 25.5, 23,
        27, 30, 28.5, 25.5,
        30, 33.5, 33, 28.5,
        33.5, 36, 35, 33,
        36, 37, 35.5, 35,
        37, 38.5, 37, 35.5,
        38.5, 40, 38.5, 37,
        40, 41.5, 40, 38.5,
        41.5, 44.5, 43, 40,
        44.5, 47, 46, 43,
        47, 49, 48, 46,
        49, 50, 49.5, 48,
        50, 52, 51, 49.5,
        52, 53, 52, 51,
        53, 54, 53.5, 52,
        54, 56, 55, 53.5,
        # other squares
        7, 11, 11, 7,
        79, 81, 81, 79,
        60.5, 67, 67, 60.5,
        61.5, 63.5, 60.5, 58.5,
        58, 59.5, 57, 55.5,
        73.5, 74.5, 72.5, 71.5,
        68.5, 69.5, 68, 67,
        66, 67, 65, 64,
        6.8, 11.2, 11.2, 6.8,
        78.5, 81.5, 81.5, 78.5,
        # hashtag
        47, 50, 50, 47,
        50, 53, 53, 50,
        50, 53, 53, 50,
        47, 50, 50, 47,
        # upper right checker arrangement
        56, 57, 55.5, 54,
        #54, 55.5, 54, 52.5,
        52.5, 54, 52, 51,
        #51, 52, 50.5, 49.5,
        # next row
        57, 58, 56, 55.5,
        55.5, 56, 54.5, 54,
        #54, 54.5, 53, 52,
        52, 53, 51.5, 50.5,
        # checker background
        56, 58, 61.5, 67, 70, 86, 86, 70.5, 67, 65, 63.5, 57.5, 53.5, 49.5,
        # next row
        65, 66, 64.5, 63.5,
        63.5, 64.5, 62.5, 61.5,
        60, 60.5, 59, 58,
        58, 59, 57, 56,
        54.5, 55.5, 54, 53,
        # next row
        66, 67, 65, 64.5,
        # SHOULD BE ON LAYER 3
        62, 63.5, 62, 60.5,
        # next row
        60, 61, 57.5, 57,
        # next row
        66, 67, 65.5, 64.5,
        # next row
        69, 70, 68.5, 67,
        65.5, 67, 65.5, 64,
        # top row 3 + 1
        74, 75, 73, 72,
        79, 80.5, 78.5, 77.5,
        84, 85.5, 83.5, 82.5,
        68, 69.5, 68, 66.5,
        # diagonal strip
        23, 24.5, 23.5, 22.5,
        24.5, 27.5, 27, 23.5,
        27.5, 33, 33, 27,
        33, 36, 35.5, 33,
        36, 45.5, 45, 35.5,
        45.5, 61.5, 61, 45,
        # boomerang
        44, 61, 50, 60,
        48, 60, 50, 59.5
        )
) %>%
  mutate(id = as.factor(id))

quads <- full_join(coords, shapes, by = "id")

quads_l <- make_layers(quads)
lines <- tribble(~grouping, ~x, ~xend, ~y, ~yend, ~thickness, ~order,
                 1, 28, 72, 44, 57, 1, 1,
                 2, 46, 68, 8, 22, 1, 1,
                 2, 50, 66, 3, 17.7, 1, 1,
                 3, 65, 78, 16, 15.6, 1, 1,
                 3, 65, 77, 18.4, 18, 1, 1,
                 4, 68, 88, 10, 84, 1, 1,
                 4, 88, 111, 84, 6, 1, 2,
                 5, 37, 55.5, 45, 64.5, 1, 1,
                 5, 55.5, 90, 64.5, 34, 1, 1,
                 6, 42, 49.5, 28, 47, 1, 1,
                 6, 49.5, 62, 47, 36, 1, 1,
                 6, 46, 59, 26, 46, 1, 1,
                 6, 59, 65, 46, 40, 1, 1,
                 6, 46.5, 70, 25.5, 49.5, 1, 1,
                 6, 70, 72, 49.5, 48, 1, 1,
                 6, 49, 77, 24, 56, 1, 1,
                 6, 77, 78, 56, 55, 1, 1,
                 6, 44, 53, 34, 25, 1, 1,
                 6, 45.5, 55.5, 37, 28.5, 1, 1,
                 6, 46.5, 59, 39.5, 33, 1, 1,
                 6, 48, 61, 43, 35, 1, 1,
                 6, 57, 63, 43, 37, 1, 1,
                 6, 58, 64, 44, 39, 1, 1,
                 6, 66, 68, 45.5, 43, 1, 1,
                 6, 68, 70, 48, 46, 1, 1,
                 6, 72, 73, 50, 49, 1, 1,
                 6, 74, 75, 52, 51, 1, 1,
                 6, 75, 76, 53, 52, 1, 1,
                 6, 76, 77, 54.5, 53.5, 1, 1,
                 6, 51, 78, 23, 55, 1, 1, 
                 7, 55.5, 120, 28.5, 27.6, 1, 1, 
                 # new page
                 8, 87.5, 87.5, 0, 32, 1, 1, 
                 9, 85, 99, 12, 27, 1, 1, 
                 10, 85, 99, 12, 27, 1, 1, 
                 10, 84.5, 98.5, 12.5, 27.5, 1, 1, 
                 10, 84, 98, 13, 28, 1, 1, 
                 11, 83.5, 98, 19.5, 33.5, 1, 1, 
                 11, 83, 97.5, 20, 34, 1, 1, 
                 11, 82.5, 97, 20.5, 34.5, 1, 1, 
                 12, 104, 118.5, 10.5, 11, 1, 1, 
                 12, 97, 117.5, 20, 20, 1, 1, 
                 13, 120, 98, 15, 36, 2, 1,
                 14, 132, 107, 36, 44.5, 3, 1,
                 15, 128, 131.5, 8.5, 8.5, 1.5, 1,
                 15, 128, 131.5, 9.1, 9.1, 1, 1,
                 16, 52, 86, 28, 38, 3, 1,
                 17, 53.5, 90, 33, 40, 3, 1,
                 # return to upper left
                 18, 55.5, 55.5, 56, 78, 1, 1,
                 18, 53, 61, 68.6, 68.9, 1, 1,
                 18, 52, 60, 70, 70.4, 1, 1,
                 18, 51, 66, 77, 70, 1, 1,
                 # bookmark
                 19, 76, 98, 78, 64, 1, 1,
                 19, 77, 98, 73, 64, 1, 1,
                 20, 78, 91, 54.5, 60.5, 1, 1,
                 20, 78.5, 91.5, 54, 60, 1, 1,
                 21, 77, 98, 51, 62, 1, 1, # needs orange right above
                 21, 88, 101.5, 52, 64, 1, 1,
                 21, 84.5, 102.5, 46.5, 63.5, 1, 1,
                 21, 91.5, 101.5, 51, 58.5, 1, 1, # needs yellow right below
                 22, 99.5, 99.5, 70, 86, 1, 1,
                 # top right checkerboard cluster
                 23, 102.5, 142, 65, 86, 1, 1,
                 23, 104, 147, 63.5, 86, 1, 1,
                 23, 105, 151, 62, 86, 1, 1,
                 23, 106, 124, 60, 69.5, 1, 1,
                 23, 104, 134, 56, 73, 1, 1,
                 23, 105, 109.5, 54.5, 57, 1, 1,
                 23, 111.5, 134, 58.5, 73, 1, 1,
                 23, 106, 110, 53, 55.5, 1, 1,
                 23, 112, 122.5, 57, 63.5, 1, 1,
                 23, 107, 112, 51, 54, 1, 1,
                 23, 108, 113, 49.5, 52.5, 1, 1,
                 23, 104, 137, 56, 4, 1, 1,
                 23, 105.5, 125, 57, 26.5, 1, 1,
                 23, 108, 125, 56, 26.5, 1, 1,
                 23, 102.5, 124, 65, 32, 1, 1,
                 23, 104.5, 124, 66.3, 32, 1, 1,
                 23, 106, 126.5, 67, 33.5, 1, 1,
                 23, 108, 126.5, 68, 33.5, 1, 1,
                 23, 109.5, 113, 69, 64, 1, 1,
                 23, 111.5, 116.5, 70, 65.5, 1, 1,
                 23, 119, 120, 74, 72, 1, 1,
                 23, 121, 122, 75, 73, 1, 1,
                 23, 129, 130, 79, 77.5, 1, 1,
                 23, 131.5, 132.5, 80.5, 78.5, 1, 1,
                 23, 138.5, 140, 84, 82.5, 1, 1,
                 23, 141, 142, 85.5, 83.5, 1, 1,
                 24, 80.5, 131, 23, 61.5, 1, 1,
                 24, 81, 131.5, 22.5, 61, 1, 1)

horizontal_lines <- tibble(grouping = c(rep(0, 6), rep(22, 5)),
       x = c(rep(124, 3), seq(129, 135, 3), rep(98, 3), rep(97.5, 2)),
       xend = c(rep(139, 3), seq(129, 135, 3), rep(101.7, 5)),
       y = c(seq(47, 53, 3), rep(42, 3), seq(71.1, 71.9, length.out = 3), seq(82.5, 82.9, length.out = 2)),
       yend = c(seq(47, 53, 3), rep(57, 3), seq(71.2, 72, length.out = 3), seq(82.6, 83, length.out = 2)),
       thickness = c(rep(1, 6), rep(0.5, 5)),
       order = rep(1, 11))

lines <- bind_rows(lines, horizontal_lines) %>%
  mutate(color = "black", 
         thickness = ifelse(thickness > 1, thickness, thickness / 2))

lines_l <- make_layers(lines)
make_half_circle <- function(id, center = c(0, 0), diameter = 1, thickness = 1, order = 1,
                             npoints = 100, start = 0, end = 1, color = "black", color_cat = "black",
                             grouping = 1){
  tt <- seq(start*pi, end*pi, length.out=npoints)
  data.frame(x = center[1] + diameter / 2 * cos(tt), 
             y = center[2] + diameter / 2 * sin(tt),
             id = id,
             thickness = thickness,
             order = order,
             color = color,
             color_cat = color_cat,
             grouping = grouping)
}

semicircle_stroke <- bind_rows(
  make_half_circle(1, c(72, 28), 10.5, grouping = 1),
  make_half_circle(2, c(82.5, 28), 10.5, 0.5, grouping = 1),
  #make_half_circle(3, c(93, 28), 10.5, 0.5),
  make_half_circle(4, c(103.5, 28), 10.5, 0.75, grouping = 1),
  make_half_circle(5, c(77.25, 22.75), 10.5, 0.5, grouping = 2),
  make_half_circle(6, c(87.75, 22.75), 10.5, 0.5, grouping = 2),
  make_half_circle(7, c(93, 17.5), 10.5, 0.5, grouping = 3),
  make_half_circle(8, c(101, 79), 5, start = -1.5, end = -.5, grouping = 4),
  make_half_circle(9, c(88, 49), 8, start = 0.25, end = 1.25, grouping = 5),
  make_half_circle(10, c(93.5, 53), 6, start = 0.25, end = 1.25, grouping = 5),
  make_half_circle(11, c(97, 57), 4.5, start = 0.25, end = 1.25, grouping = 5),
  make_half_circle(12, c(100, 59.5), 3, start = 0.25, end = 1.25, grouping = 5),
  # bookmark
  make_half_circle(13, c(92, 58), 52, start = 0.98, end = 0.65, grouping = 6),
  make_half_circle(14, c(87, 58), 40, start = 1, end = 0.65, grouping = 6),
  # funky circles
  make_half_circle(15, c(63, 42), 2, 0.5, start = 0.7, end = 0.35, grouping = 7),
  make_half_circle(16, c(55.5, 43.5), 3.1, 2, start = 1.2, end = -0.2, grouping = 7),
  make_half_circle(17, c(44, 32), 11.5, 0.5, start = 1.15, end = 0.4, grouping = 7)
  ) %>%
  mutate(thickness = ifelse(thickness > 1, thickness, thickness / 2))
  

semicircle_stroke_color <- bind_rows(
  make_half_circle(1, c(72,28), 10, color = "#83809F", color_cat = "blue", grouping = 1),
  make_half_circle(2, c(82.5, 28), 10, color = "#C9BE3B", color_cat = "yellow", grouping = 1),
  make_half_circle(3, c(93, 28), 10, color = "#A62115", color_cat = "red", grouping = 1),
  make_half_circle(5, c(77.25, 22.75), 10, color = "#D1A0A0", color_cat = "pink", grouping = 1),
  make_half_circle(6, c(88, 49), 8.5, start = 0.25, end = 1.25, color = "#D47B76", color_cat = "pink", grouping = 2),
  make_half_circle(7, c(93.5, 53), 6.5, start = 0.25, end = 1.25, color = "#A2112D", color_cat = "red", grouping = 2),
  make_half_circle(8, c(97, 57), 5, start = 0.25, end = 1.25, color = "#AF3241", color_cat = "pink", grouping = 2),
  make_half_circle(9, c(100, 59.5), 3.5, start = 0.25, end = 1.25, color = "#C15E6A", color_cat = "pink", grouping = 2),
  make_half_circle(10, c(103, 46), 8, 2, start = -0.35, end = 0.65, color = "#C56D62", color_cat = "pink", grouping = 3),
  # funky circles
  make_half_circle(11, c(63, 43), 4, 3, start = 0.85, end = 0.22, color = "#99ABBD", color_cat = "blue", grouping = 4),
  make_half_circle(12, c(63.5, 43), 6, start = 0.85, end = 0.25, color = "#E0C6BE", color_cat = "pink", grouping = 4),
  make_half_circle(13, c(63.5, 43), 6.5, start = 0.85, end = 0.25, color = "#A4B89E", color_cat = "green", grouping = 4),
  make_half_circle(14, c(55.5, 43.5), 3.5, 2, start = 1.2, end = -0.2, 
                   color = "#3F5F47", color_cat = "green", grouping = 5),
  make_half_circle(15, c(55.5, 44), 5, start = 1, end = 0, color = "#D2A3B5", color_cat = "pink", grouping = 5),
  make_half_circle(16, c(55.5, 44), 5.3, start = 1, end = 0, color = "#D5DBB4", color_cat = "green", grouping = 5),
  make_half_circle(17, c(44, 32), 11.7, 0.5, start = 1.15, end = 0.4, 
                   color = "#EBEDA0", color_cat = "yellow", grouping = 6)
  ) %>%
  mutate(thickness = ifelse(thickness > 1, thickness, thickness / 2))

semicircle_fill <- bind_rows(
  make_half_circle(1, c(72,28), 10.5, color = "#EDE5E3", color_cat = "white", grouping = 1),
  make_half_circle(2, c(82.5, 28), 10.5, color = "#F3EEEE", color_cat = "white", grouping = 1),
  make_half_circle(3, c(93, 28), 10.5, color = "#F4F1EE", color_cat = "white", grouping = 1),
  make_half_circle(4, c(103.5, 28), 10.5, color = "#F6F5F6", color_cat = "white", grouping = 1),
  make_half_circle(5, c(103, 46), 2, start = -0.35, end = -1.35, color = "#F5EDF1", color_cat = "white", grouping = 2),
  make_half_circle(6, c(103, 46), 2, start = -0.35, end = 0.65, color = "#111112", color_cat = "black", grouping = 2),
  make_half_circle(7, c(103, 46), 5, start = -0.35, end = 0.65, color = "#DADBEB", color_cat = "grey",
                   order = 2, grouping = 3),
  make_half_circle(8, c(103, 46), 6, order = 3, start = -0.35, end = 0.65, 
                   color = "#CEC161", color_cat = "yellow", grouping = 3),
  # funky circles
  (make_half_circle(9, c(63.5, 43), 6.5, start = 0.85, end = 0.25, 
                    color = "#EEEBF1", color_cat = "white", grouping = 1) %>%
     add_row(id = 9, x = 63, y = 42.5, order = 1, color = "#EEEBF1", color_cat = "white", grouping = 4)),
  (make_half_circle(10, c(55.5, 44), 5.3, start = 1, end = 0, color = "#E2D9D6", color_cat = "white", grouping = 1) %>%
     add_row(id = 10, x = 56, y = 41, order = 1, color = "#E2D9D6", color_cat = "white", grouping = 5))
  ) %>%
  mutate(alpha = 1)

semicircle_fill_l <- make_layers(semicircle_fill)
semicircle_stroke_l <- make_layers(semicircle_stroke)
semicircle_stroke_color_l <- make_layers(semicircle_stroke_color)
saveRDS(circles_l, "data/circles.rds")
saveRDS(lines_l, "data/lines.rds")
saveRDS(quads_l, "data/quads.rds")
saveRDS(semicircle_fill_l, "data/semicircle-fill.rds")
saveRDS(semicircle_stroke_l, "data/semicircle-stroke.rds")
saveRDS(semicircle_stroke_color_l, "data/semicircle-stroke-color.rds")
saveRDS(triangles_l, "data/triangles.rds")
plot_circles <- function(layers = c(), execute = TRUE){
  if(execute == FALSE) {
    return(p)
  } else{
    for(i in layers){
    p <- p +
    new_scale_fill() +
    new_scale_color() +
    geom_circle(data = as.data.frame(circles_l[[1]][[i]]), 
              aes(x0 = x, y0 = y, r = radius, fill = color, color = color, alpha = alpha)) +
    scale_fill_manual(values = unique(circles_l[[2]][[i]][[1]])) +
    scale_color_manual(values = unique(circles_l[[2]][[i]][[1]]))
  }
    return(p)
  }
}

plot_semicircles <- function(layers = c(), execute = TRUE){
  if(execute == FALSE) {
    return(p)
  } else{
    for(i in layers){ 
      p <- p + 
      new_scale_fill() +
      new_scale_color() +
      geom_polygon(data = as.data.frame(semicircle_fill_l[[1]][[i]]), 
                 aes(x = x, y = y, group = id, fill = color, alpha = alpha)) +
      scale_fill_manual(values = unique(semicircle_fill_l[[2]][[i]][[1]])) 
    }
  
  return(p)
  }
}

plot_quads <- function(layers = c(), execute = TRUE){
  if(execute == FALSE) {
    return(p)
  } else{
    for(i in layers){
      p <- p +
      new_scale_fill() +
      new_scale_color() +
      geom_polygon(data = as.data.frame(quads_l[[1]][[i]]), 
                 aes(x = x, y = y, group = id, fill = color, alpha = alpha)) +
      scale_fill_manual(values = unique(quads_l[[2]][[i]][[1]]))
    }
    
    return(p)
  }
}

plot_triangles <- function(layers = c(), execute = TRUE){
  if(execute == FALSE) {
    return(p)
  } else{
    for(i in layers){
      p <- p + 
      new_scale_fill() +
      new_scale_color() +
      geom_polygon(data = as.data.frame(triangles_l[[1]][[i]]), 
                 aes(x = x, y = y, group = id, fill = color, alpha = alpha)) +
      scale_fill_manual(values = unique(triangles_l[[2]][[i]][[1]]))
    }
    
    return(p)
  }
}

plot_lines <- function(layers = c(), execute = TRUE){
  if(execute == FALSE) {
    return(p)
  } else{
    for(i in layers){
      p <- p + 
      #new_scale_fill() +
      #new_scale_color() +
      geom_segment(data = as.data.frame(lines_l[[1]][[i]]), 
                 aes(x = x, xend = xend, y = y, yend = yend, size = thickness ^ 2))
    }
    
    return(p)
  }
}

plot_semicircle_stroke <- function(layers = c(), execute = TRUE){
  if(execute == FALSE) {
    return(p)
  } else{
    for(i in layers){
      p <- p + 
      new_scale_fill() +
      new_scale_color() +
      geom_path(data = as.data.frame(semicircle_stroke_l[[1]][[i]]), 
                aes(x = x, y = y, group = id, color = color, size = thickness ^ 2)) +
      scale_color_manual(values = unique(semicircle_stroke_l[[2]][[i]][[1]]))
    }
    
    return(p)
  }
}

plot_semicircle_stroke_color <- function(layers = c(), execute = TRUE){
  if(execute == FALSE) {
    return(p)
  } else{
    for(i in layers){
      p <- p + 
      new_scale_fill() +
      new_scale_color() +
      geom_path(data = as.data.frame(semicircle_stroke_color_l[[1]][[i]]), 
              aes(x = x, y = y, group = id, color = color, size = thickness ^ 2)) +
    scale_color_manual(values = unique(semicircle_stroke_color_l[[2]][[i]][[1]]))
    }
    
    return(p)
  }
}
clip <- function(x, low, high) {
  x[x < low] <- low
  x[x > high] <- high
  return(x)
}

add_noise <- function(df, layers = c(1), magnitude = 5){
  for (i in layers){
    df[[1]][[i]] <- as.data.frame(df[[1]][[i]]) %>%
      group_by(grouping) %>%
      mutate(noise_x = rnorm(1, 0, magnitude),
             noise_y = rnorm(1, 0, magnitude)) %>%
      ungroup() %>%
      mutate(across(contains("x"), ~ clip(.x + noise_x, xmin, xmax)),
             across(contains("y"), ~ clip(.x + noise_y, ymin, ymax)))
  }
  return(df)
}

xmin <- 0
xmax <- 152
ymin <- 0
ymax <- 86
circles_l <- readRDS("data/circles.rds")
lines_l <- readRDS("data/lines.rds")
quads_l <- readRDS("data/quads.rds")
semicircle_fill_l <- readRDS("data/semicircle-fill.rds")
semicircle_stroke_l <- readRDS("data/semicircle-stroke.rds")
semicircle_stroke_color_l <- readRDS("data/semicircle-stroke-color.rds")
triangles_l <- readRDS("data/triangles.rds")

circles_l <- add_noise(circles_l, c(1:6)) lines_l <- add_noise(lines_l, c(1:2)) quads_l <- add_noise(quads_l, c(1:4)) semicircle_fill_l <- add_noise(semicircle_fill_l) semicircle_stroke_l <- add_noise(semicircle_stroke_l) semicircle_stroke_color_l <- add_noise(semicircle_stroke_color_l) triangles_l <- add_noise(triangles_l, c(1:6))

lapply(c(quads_l, circles_l, lines_l, semicircle_fill_l, semicircle_stroke_color_l, semicircle_stroke_l, triangles_l), add_noise)

map(c(quads_l, circles_l, lines_l, semicircle_fill_l, semicircle_stroke_color_l, semicircle_stroke_l, triangles_l), add_noise)

lines_l <- add_noise(lines_l, c(1:2), 30)

-other applys and maps -reduce list hierarchy

p <- ggplot() +
  scale_alpha(range = c(0.999, 1)) +
  scale_size(range = c(0.3, 1.3)) +
  coord_fixed(xlim = c(xmin, xmax), ylim = c(ymin, ymax), expand = FALSE) +
  theme_nothing() +
  labs(x = NULL, y = NULL) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "#F1E8DC", color = "#F1E8DC"))


p <- plot_triangles(c(6:3))
p <- plot_semicircles(c(3:1))
p <- plot_semicircle_stroke_color(c(1))
p <- plot_semicircle_stroke(c(1))
p <- plot_quads(c(4:1))
p <- plot_circles(c(6:2))
p <- plot_triangles(c(2:1))
p <- plot_circles(c(1))
p <- plot_lines(c(2:1))

p

To do: -fix layering -create background of geom_point

Make Shiny:

, make_half_circle(18, c(52, 67), 15, start = 0, end = -0.65, color = “#0E0E10”, color_cat = “black”), make_half_circle(19, c(51.5, 66), 17, start = 0.05, end = -0.6, color = “#0E0E10”, color_cat = “black”)

, (bind_rows(make_half_circle(18, c(52, 67), 15, start = 0, end = -0.65, color = “red”, color_cat = “black”), make_half_circle(18, c(51.5, 66), 17, start = 0.05, end = -0.6, color = “red”, color_cat = “black”)) %>% add_row(id = 18, x = 49, y = 58, order = 1, color = “red”, color_cat = “black”))

References for ggcircle https://datavizpyr.com/annotate-a-plot-with-circle-ggplot2/ multiple manual color scales https://pythonawesome.com/multiple-fill-and-color-scales-in-ggplot2/ for half circle function https://stackoverflow.com/questions/28185743/draw-a-half-circle-with-ggplot2 https://stackoverflow.com/questions/10276092/to-find-whether-a-column-exists-in-data-frame-or-not https://stackoverflow.com/questions/38181744/r-shiny-input-slider-range-values/38181817 https://stackoverflow.com/questions/34351356/plotting-with-action-button-in-shiny-rmarkdown https://stackoverflow.com/questions/15987367/how-to-add-layers-in-ggplot-using-a-for-loop https://stackoverflow.com/questions/24049159/change-the-color-and-font-of-text-in-shiny-app https://stackoverflow.com/questions/60797216/how-to-display-default-plot-in-www-before-actionbutton-is-clicked-r-shiny